package com.file.system.controller.system;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.file.system.entity.res.CommonResult;
import com.file.system.entity.system.WebMenu;
import com.file.system.service.system.IWebMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import java.util.List;

import static cn.hutool.core.util.ObjectUtil.isNotEmpty;
import static cn.hutool.core.util.ObjectUtil.isNotNull;


/**
 * 菜单
 *
 * @author jch
 */
@RestController
@Validated
@RequestMapping("/web/menu")
public class WebMenuController {

   @Autowired
    private IWebMenuService service;


    /**
     * 查询菜单
     *
     * @param webMenu 菜单
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/get")
    public CommonResult<List<WebMenu>> select(
            @RequestBody @Validated WebMenu webMenu
    ) {
        QueryWrapper<WebMenu> query = new QueryWrapper<>();
        //根据sort字段升序排列
        query.orderByAsc("a.sort");
        //查询未删除的数据
        query.eq("a.is_del", 0);
        //查询pid不为空的数据
        query.eq(isNotNull(webMenu.getPid()), "a.pid", webMenu.getPid());
        //查询name不为空的数据
        query.like(isNotEmpty(webMenu.getName()), "a.name", webMenu.getName());
        //查询path不为空的数据
        query.like(isNotEmpty(webMenu.getPath()), "a.path", webMenu.getPath());
        //查询auth不为空的数据
        query.like(isNotEmpty(webMenu.getAuth()), "a.auth", webMenu.getAuth());
        //查询icon不为空的数据
        query.like(isNotEmpty(webMenu.getIcon()), "a.icon", webMenu.getIcon());
        //查询file_path不为空的数据
        query.like(isNotEmpty(webMenu.getFilePath()), "a.file_path", webMenu.getFilePath());
        //查询params不为空的数据
        query.like(isNotEmpty(webMenu.getParams()), "a.params", webMenu.getParams());
        //查询dict_menu_show不为空的数据
        query.eq(isNotEmpty(webMenu.getDictMenuShow()), "a.dict_menu_show", webMenu.getDictMenuShow());
        //查询dict_menu_is_link不为空的数据
        query.eq(isNotEmpty(webMenu.getDictMenuIsLink()), "a.dict_menu_is_link", webMenu.getDictMenuIsLink());
        //查询dict_menu_type不为空的数据
        query.eq(isNotEmpty(webMenu.getDictMenuType()), "a.dict_menu_type", webMenu.getDictMenuType());
        //查询dict_menu_status不为空的数据
        query.eq(isNotEmpty(webMenu.getDictMenuStatus()), "a.dict_menu_status", webMenu.getDictMenuStatus());
        //查询集合
        List<WebMenu> list = service.list(query);
        //返回查询结果
        return CommonResult.ok().setResult(list);
    }


    /**
     * 通过ids查询菜单集合
     *
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/listByIds")
    public CommonResult<List<WebMenu>> listByIds(@RequestBody List<Long> ids) {
        //查询集合
        List<WebMenu> list = service.listByIds(ids);
        return CommonResult.ok().setResult(list);
    }

    /**
     * 查询所有非删除状态菜单
     *
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/listAll")
    public CommonResult<List<WebMenu>> listAll() {
        //查询集合
        List<WebMenu> list = service.listAll();
        return CommonResult.ok().setResult(list);
    }


    /**
     * 通过id查询菜单
     *
     * @param id 菜单id
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/id/{id}")
    public CommonResult<WebMenu> getById(@PathVariable
                                            @Min(value = 1, message = "id长度最小1")
                                            Long id) {
        //查询集合
        WebMenu webMenu = service.getById(id);
        return CommonResult.ok().setResult(webMenu);
    }



    /**
     * 通过id删除菜单
     *
     * @param id id
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/remove/{id}")
    public CommonResult removeById(@PathVariable
                            @Min(value = 1, message = "id长度最小1")
                            Long id) {
        int count = service.count(new QueryWrapper<WebMenu>()
                .eq("a.is_del", "0")
                .eq("a.pid", id)
        );
        if (count > 0) return CommonResult.fail().setText("存在子菜单");
        boolean state = service.removeById(id);
        //返回
        return CommonResult.check(state);
    }




    /**
     * 通过id编辑菜单
     *
     * @param webMenu 菜单
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/updateById")
    public CommonResult<WebMenu> up(@RequestBody @Validated(WebMenu.update.class) WebMenu webMenu) {
        //查询
        WebMenu webMenuOld = service.getById(webMenu.getId());
        //查询该权限标识是否已存在
        if (ObjectUtil.notEqual(webMenuOld.getAuth(), webMenu.getAuth())) {
            //查询权限标识是否存在
            int count = service.count(new QueryWrapper<WebMenu>()
                    .eq("a.is_del", "0")
                    .eq("a.auth", webMenu.getAuth())
            );
            if (count > 0) return CommonResult.fail().setText("权限标识存在");
        }
        //编辑修改
        boolean state = service.updateById(webMenu);
        return CommonResult.check(state).setResult(webMenu);
    }


    /**
     * 添加菜单
     *
     * @param webMenu 菜单
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/save")
    public CommonResult<WebMenu> save(@RequestBody @Validated(WebMenu.add.class) WebMenu webMenu) {

        //查询权限标识是否存在
        int count = service.count(new QueryWrapper<WebMenu>()
                .eq("a.is_del", "0")
                .eq("a.auth", webMenu.getAuth())
        );
        if (count > 0) return CommonResult.fail().setText("权限标识存在");
        //添加菜单
        boolean state = service.save(webMenu);
        return CommonResult.check(state).setResult(webMenu);
    }
}

